import { Controller } from "../../mvc/core/Controller";
import { SWrestleResult } from "../../../Message/Public/SWrestleResult";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { DataEvent } from "../events/DataEvent";
import { EventName } from "../mvc/EventName";
import { IWrestleModule } from "../../../modules/interfaces/IWrestleModule";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { IView } from "../../mvc/interfaces/IView";
import { WrestleModule } from "../../../modules/WrestleModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { ArenaDetailControler } from "./sub/ArenaDetailControler";
import { GameProxy } from "../mvc/GameProxy";
import { Exception } from "../../../Framework/Util/Exception";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { Alert } from "../../../com/mui/controls/Alert";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { WrestlePageData } from "../view/wrestle/wrestleData/WrestlePageData";
import { PageData } from "../view/arena/ArenaData/PageData";
import { ECopyType } from "../../../Message/Public/ECopyType";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { SWrestleGetRooms } from "../../../Message/Public/SWrestleGetRooms";
import { SWrestleOpen } from "../../../Message/Public/SWrestleOpen";
import { SWrestleAccount } from "../../../Message/Public/SWrestleAccount";
import { SWrestleMatch } from "../../../Message/Public/SWrestleMatch";
import { SWrestleStart } from "../../../Message/Public/SWrestleStart";
import { SWrestleToplists } from "../../../Message/Public/SWrestleToplists";
import { SWrestleBattles } from "../../../Message/Public/SWrestleBattles";
import { SWrestleSignUp } from "../../../Message/Public/SWrestleSignUp";
import { SWrestleFightRecordInfos } from "../../../Message/Public/SWrestleFightRecordInfos";
type int = number;
//class WrestleController
    
    export  class WrestleController extends Controller
    {
       

        /* internal  */onWrestleResultHandler(arg1: SWrestleResult): void
        {
            if (arg1.isBattler) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.WrestleClearCd));
            }
            (view as IWrestleModule).wrestleResult(arg1);
            return;
        }

        /* internal  */onActiveAddWrestleMarkHandler(arg1: SActiveToClient): void
        {
            if (_view) 
            {
                (view as IWrestleModule).updateActiveAdd(arg1);
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._wrestleModule) 
            {
                this._wrestleModule = new WrestleModule();
            }
            return this._wrestleModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.showWrestleDetailPanel, this.opentArenaDetailPanelHander);
            Dispatcher.addEventListener(EventName.wrestleShowRoomWindow, this.onShowRoomWindow);
            Dispatcher.addEventListener(EventName.WrestleGetRoomList, this.onWrestleGetRoomList);
            Dispatcher.addEventListener(EventName.WrestleEnterWaitRoom, this.onEnterWaitRoom);
            Dispatcher.addEventListener(EventName.WrestleJoin, this.onWrestleJoin);
            Dispatcher.addEventListener(EventName.WrestleGetMyWrestleInfo, this.onGetMyWrestleInfo);
            Dispatcher.addEventListener(EventName.WrestleIsAccept, this.onIsAccept);
            Dispatcher.addEventListener(EventName.WrestleShowWrestleWindow, this.onShowWrestleWindow);
            Dispatcher.addEventListener(EventName.WrestleExit, this.onWrestleExit);
            Dispatcher.addEventListener(EventName.WrestleExitNow, this.onWrestleExitNow);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.WrestleGetTopList, this.onGetTopList);
            Dispatcher.addEventListener(EventName.WrestleGetWrestleBattles, this.onGetWrestleBattles);
            Dispatcher.addEventListener(EventName.WrestleLookBattle, this.onLookBattle);
            Dispatcher.addEventListener(EventName.WrestleClearCd, this.onWrestleClearCd);
            Dispatcher.addEventListener(EventName.WrestleGetViedoList, this.onWrestleGetViedoList);
            Dispatcher.addEventListener(EventName.WrestleSaveVideo, this.onSaveVideo);
            Dispatcher.addEventListener(EventName.WrestleWatchViedo, this.onWrestleWatchVideo);
            Dispatcher.addEventListener(EventName.WrestleGetPlayerInfo, this.onWrestleGetPlayerInfo);
            NetDispatcher.addCmdListener(ServerCommand.WrestleWillOpenCommand, this.onWrestleWillOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleRoomsCommand, this.onWrestleRoomsHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleOpenCommand, this.onWrestleOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleCloseCommand, this.onWrestleCloseHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleMyWrestleInfoUpdate, this.onWrestleMyWrestleInfoUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleAccountUpdate, this.onWrestleAccountUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleMatchCommand, this.onWrestleMatchHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleResultCommand, this.onWrestleResultHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleStartCommand, this.onWrestleStartHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleTopListUpdate, this.onTopListUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleBattlesCommand, this.onWrestleBattlesHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleSignUpStateCommand, this.onWrestleSignUpStateHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdate);
            NetDispatcher.addCmdListener(ServerCommand.WrestleFightRecordInfosCommand, this.onWrestleFightRecordHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveAddWrestleMark, this.onActiveAddWrestleMarkHandler);
            return;
        }

        /* internal  */opentArenaDetailPanelHander(arg1: Object /* flash.events.Event */): void
        {
            if (!this._arenaDetailControler) 
            {
                this._arenaDetailControler = new ArenaDetailControler();
            }
            this._arenaDetailControler.showView();
            return;
        }

        /* internal  */onEnterWaitRoom(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.wrestle.enterWrestle(true, loc1, 0, this.enterWaitRoomFailed);
            return;
        }

        /* internal  */enterWaitRoomFailed(arg1: Exception): void
        {
            GameProxy.wrestle.getWrestleRooms();
            return;
        }

        /* internal  */onShowRoomWindow(arg1: DataEvent): void
        {
            (view as IWrestleModule).showRoomWindow();
            return;
        }

        /* internal  */onWrestleGetRoomList(arg1: DataEvent): void
        {
            GameProxy.wrestle.getWrestleRooms();
            return;
        }

        /* internal  */onWrestleJoin(arg1: DataEvent): void
        {
            var loc1=Boolean(arg1.data);
            if (!cache.wrestle.isOpen) 
            {
                MsgManager.showRollTipsMsg(Language.getString(42200));
                return;
            }
            GameProxy.wrestle.signUpWrestle(loc1);
            return;
        }

        /* internal  */onGetMyWrestleInfo(arg1: DataEvent): void
        {
            GameProxy.wrestle.getWrestleMyInfo();
            return;
        }

        /* internal  */onIsAccept(arg1: DataEvent): void
        {
            GameProxy.wrestle.enterWrestle(false, 0, 0);
            return;
        }

        /* internal  */onShowWrestleWindow(arg1: DataEvent): void
        {
            (view as IWrestleModule).showWrestleWindow();
            return;
        }

        /* internal  */onWrestleExit(arg1: DataEvent): void
        {
            Alert.show(Language.getString(42201), null, Alert.OK | Alert.CANCEL, null, this.onExitWrestle);
            return;
        }

        /* internal  */onWrestleExitNow(arg1: DataEvent): void
        {
            this.exitWrestle();
            return;
        }

        /* internal  */onExitWrestle(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.exitWrestle();
            }
            return;
        }

        /* internal  */exitWrestle(): void
        {
            GameProxy.wrestle.enterWrestle(true, 0, 0);
            return;
        }

        public isInitView(): Boolean
        {
            return !(this._wrestleModule == null);
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (GameMapUtil.curMapState.isWrestleWaitingRoom) 
            {
                (view as IWrestleModule).showRoomWindow(false);
            }
            if (GameMapUtil.curMapState.isWrestle) 
            {
                this._isInWrestleMap = true;
                (view as IWrestleModule).enterWrestle();
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyEnterCopyMap));
                Dispatcher.dispatchEvent(new DataEvent(EventName.WrestleClearCd));
            }
            else if (this._isInWrestleMap) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveCopyMap));
                (view as IWrestleModule).leaveWrestle();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Role_Relive_City));
                this._isInWrestleMap = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.WrestleExitMap));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
            }
            return;
        }

        /* internal  */clearCd(): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Skill_CoolDown_Clear));
            Dispatcher.dispatchEvent(new DataEvent(EventName.ItemMomentDrug_CoolDown_Clear));
            return;
        }

        /* internal  */onGetTopList(arg1: DataEvent): void
        {
            var loc1=arg1.data as WrestlePageData;
            GameProxy.wrestle.getWrestleToplist(loc1.pageNum, loc1.pageSize, loc1.orderObjType, loc1.onlyMyServer, loc1.isShowMyPos);
            return;
        }

        /* internal  */onGetWrestleBattles(arg1: DataEvent): void
        {
            GameProxy.wrestle.getWrestleBattles();
            return;
        }

        /* internal  */onLookBattle(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            GameProxy.wrestle.enterWrestle(false, 0, loc1);
            return;
        }

        /* internal  */onWrestleClearCd(arg1: DataEvent): void
        {
            this.clearCd();
            return;
        }

        /* internal  */onWrestleGetViedoList(arg1: DataEvent): void
        {
            var loc1=arg1.data as PageData;
            GameProxy.wrestle.getWrestleFightRecordInfos(loc1.pageNum, loc1.pageSize);
            return;
        }

        /* internal  */onSaveVideo(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.wrestle.saveWrestleFightRecord(loc1);
            return;
        }

        /* internal  */onWrestleWatchVideo(arg1: DataEvent): void
        {
            var e: DataEvent;
            var recordId: string;
            var onCloseAlert: Function;

            var loc1;
            recordId = null;
            onCloseAlert = null;
            e = arg1;
            onCloseAlert = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    GameProxy.arena.watchFightRecord(ECopyType._ECopyWrestle, recordId);
                }
                return;
            }
            recordId = e.data as string;
            if (cache.wrestle.wrestleSignUp && !cache.wrestle.wrestleSignUp.isCancel) 
            {
                Alert.show(Language.getString(42351), null, Alert.OK | Alert.CANCEL, null, onCloseAlert);
            }
            else 
            {
                GameProxy.arena.watchFightRecord(ECopyType._ECopyWrestle, recordId);
            }
            return;
        }

        /* internal  */onWrestleGetPlayerInfo(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            GameProxy.wrestle.getWrestlePlayerInfo(loc1);
            return;
        }

        /* internal  */onWrestleWillOpenHandler(arg1: Object): void
        {
            if (cache.role.entityInfo.level >= LevelLimitDefin.ArenaMinLevel) 
            {
                (view as IWrestleModule).wrestleTips();
            }
            return;
        }

        /* internal  */onWrestleRoomsHandler(arg1: SWrestleGetRooms): void
        {
            (view as IWrestleModule).showRooms(arg1);
            return;
        }

        /* internal  */onWrestleOpenHandler(arg1: SWrestleOpen): void
        {
            this.onWrestleWillOpenHandler(null);
            (view as IWrestleModule).open(arg1);
            return;
        }

        /* internal  */onWrestleCloseHandler(arg1: Object): void
        {
            (view as IWrestleModule).close();
            if (this._arenaDetailControler) 
            {
                this._arenaDetailControler.dispose();
                this._arenaDetailControler = null;
            }
            return;
        }

        /* internal  */onWrestleMyWrestleInfoUpdateHandler(arg1: Object): void
        {
            (view as IWrestleModule).updateMyWrestleInfo(cache.wrestle.wrestleMyWrestleinfo);
            return;
        }

        /* internal  */onWrestleAccountUpdateHandler(arg1: SWrestleAccount): void
        {
            (view as IWrestleModule).updateMyWrestleInfo(cache.wrestle.wrestleMyWrestleinfo);
            (view as IWrestleModule).updateAccount(arg1);
            return;
        }

        /* internal  */onWrestleMatchHandler(arg1: SWrestleMatch): void
        {
            (view as IWrestleModule).wrestleMatch(arg1);
            return;
        }

        /* internal  */onWrestleStartHandler(arg1: SWrestleStart): void
        {
            (view as IWrestleModule).wrestleStart(arg1);
            MsgManager.showTaskTarget(Language.getString(42202));
            return;
        }

        /* internal  */onTopListUpdateHandler(arg1: SWrestleToplists): void
        {
            (view as IWrestleModule).wrestleUpdateTopList(arg1);
            return;
        }

        /* internal  */onWrestleBattlesHandler(arg1: SWrestleBattles): void
        {
            (view as IWrestleModule).wrestleUpdateBattles(arg1);
            return;
        }

        /* internal  */onWrestleSignUpStateHandler(arg1: SWrestleSignUp): void
        {
            (view as IWrestleModule).updateSignUpState(arg1);
            return;
        }

        /* internal  */onMoneyUpdate(arg1: Object=null): void
        {
            if (this.isInitView()) 
            {
                (view as IWrestleModule).updateArenaMoney(cache.role.money.arena);
            }
            return;
        }

        /* internal  */onWrestleFightRecordHandler(arg1: SWrestleFightRecordInfos): void
        {
            if (this.isInitView()) 
            {
                (view as IWrestleModule).updateVideoList(arg1);
            }
            return;
        }

        private /* var */_wrestleModule: IWrestleModule;

        private /* var */_arenaDetailControler: ArenaDetailControler;

        private /* var */_isInWrestleMap: Boolean=false;
    }
